package dragon.tagext.util;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import dragon.core.exception.ExceptionHandler;
import dragon.core.util.DateUtils;
import dragon.core.util.StringUtils;

public class DateFormat extends SimpleTagSupport {

	/**
	 * 日期时间字符串
	 */
	private String value;

	/**
	 * 原有日期时间格式
	 */
	private String spattern;

	/**
	 * 目标日期时间格式
	 */
	private String tpattern;

	public void setSpattern(String spattern) {
		this.spattern = spattern;
	}

	public void setTpattern(String tpattern) {
		this.tpattern = tpattern;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public void doTag() throws JspException, IOException {
		JspContext jspContext = getJspContext();
		JspWriter out = jspContext.getOut();
		try {
			if (!StringUtils.isEmpty(value)) {
				out.print(DateUtils.format(DateUtils.parse(value, spattern), tpattern));
			}
		} catch (ExceptionHandler e) {
			throw new JspException(e);
		}
	}

}
